package com.blog.cloud.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.blog.cloud.mybatis.handler.MybatisPlusTenantHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

/**
 * MyBatis-Plus 3.5.1 拦截器
 *
 * @author 龙茶清欢
 * @since 2023-02-19 08:04:26
 */
@MapperScan(basePackages = "com.blog.cloud.*.mapper")
public class MybatisPlusConfig {

    @Autowired
    private MybatisPlusTenantHandler mybatisPlusTenantHandler;

    /**
     * 新版分页插件设置，可根据需求选择添加
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        // 插件主体，封装其他场景插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 自动分页插件，以 MYSQL 为数据库
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // sql 性能规范插件
        interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        // 防止全表更新与删除插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        // 多租户插件
        TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
        tenantInterceptor.setTenantLineHandler(mybatisPlusTenantHandler);
        interceptor.addInnerInterceptor(tenantInterceptor);
        return interceptor;
    }
    
}

